﻿using System;

using System.Data.SqlClient;

namespace IFD2
{
    public partial class PlanRoute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("NA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["flt_no"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }
                if (Request.QueryString["id"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }

                lblAirline.Text = Session["AIRLINE"].ToString();
                lblFltNo.Text = Request.QueryString["flt_no"].ToString();
                lblID.Text = Request.QueryString["id"].ToString();

                //KEY.Value = lblAirline.Text + "|" + lblFltNo.Text + "|" + lblID.Text;

            }
            lblError.Text = "";
            btnGenSegData.Attributes.Add("onclick", "javascript:return confirm('Are you sure the route information is ready?');");
            btnClear.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            txtAirport.Text = txtAirport.Text.Trim().ToUpper();
            if (txtAirport.Text.Length < 3)
            {
                lblError.Text = "Invalid airport code!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_plan_add_route");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@id", lblID.Text);
                para = cmd.Parameters.AddWithValue("@stop_sn", lstTripSn.SelectedValue);
                para = cmd.Parameters.AddWithValue("@airport", txtAirport.Text);
                para = cmd.Parameters.AddWithValue("@terminal", txtTerminal.Text);
                //para = cmd.Parameters.AddWithValue("@dep_date_diff", lstDepDateDiff.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@dep_time", txtDepTime.Text);
                para = cmd.Parameters.AddWithValue("@ground_minutes", txtGroundMinutes.Text);
                para = cmd.Parameters.AddWithValue("@fly_minutes", txtMinutes.Text);
                //para = cmd.Parameters.AddWithValue("@arr_date_diff", lstArrDateDiff.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@arr_time", txtArrTime.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                txtTerminal.Text = "";

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Airport not found!";
                else if (ret_code == 2)
                    lblError.Text = "Insert failed!";
                else if (ret_code == 3)
                    lblError.Text = "Can not edit route when the plan is in current status!";
                else if (ret_code == 4)
                    lblError.Text = "Can not edit route when the plan is in current stage!";
                else if (ret_code == 5)
                    lblError.Text = "Please just input HH:MM!";
                //else if (ret_code == 6)
                //    lblError.Text = "Illegal fly minutes!";
                else
                    lblError.Text = "Error!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY2"] == null)
            {
                lblError.Text = "Please select the record you want to delete.";
                return;
            }
            key = Request.Form["KEY2"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_delete_route";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@id", strArray[2]);
                para = cmd.Parameters.AddWithValue("@sn", strArray[3]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Delete failed!";
                else if (ret_code == 2)
                    lblError.Text = "Can not edit route when the plan is in current status!";
                else if (ret_code == 3)
                    lblError.Text = "Please clear inventory data first if you want to change route!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnGenSegData_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_plan_generate_sub_tables2");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@id", lblID.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    //lblError.Text = "Done.";
                    Response.Redirect("PlanInventory.aspx?flt_no=" + lblFltNo.Text + "&id=" + lblID.Text);
                }
                else if (ret_code == 1)
                    lblError.Text = "Country not found!";
                else if (ret_code == 2)
                    lblError.Text = "Generating segments failed!";
                else if (ret_code == 3)
                    lblError.Text = "Failed! Check plan status please.";
                else if (ret_code == 4)
                    lblError.Text = "Failed! Please clear inventory data first.";
                else if (ret_code == 5)
                    lblError.Text = "Failed! Please clear inventory data first.";
                else if (ret_code == 6)
                    lblError.Text = "Failed! Please input plan route first.";
                else if (ret_code == 7)
                    lblError.Text = "Generating inventory falied!";
                else if (ret_code == 8)
                    lblError.Text = "Route data not found!";
                else if (ret_code == 9)
                    lblError.Text = "Stop S/N is not sequential!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();


            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_clear_sub_tables";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@id", lblID.Text);

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                    lblError.Text = "Done.";
                else if (ret_code == 1)
                    lblError.Text = "Failed! Check plan status.";
                else
                    lblError.Text = "error:" + ret_code.ToString() + ". Delete failed!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
    }
}